﻿#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include "SDL/SDL.h"
#include "ite/itp.h"
#include "ite/itu.h"
#include "wifiMgr.h"

static bool screenOff,doubleclick;

static float screenSaverCountDown,screenSaverCountDownAgain;
static uint32_t screenSaverLastTick;

void ScreenInit(void)
{
    screenSaverLastTick = SDL_GetTicks();
    screenOff = false;
	doubleclick = false;
}

void ScreenSetBrightness(int value)
{
    ioctl(ITP_DEVICE_BACKLIGHT, ITP_IOCTL_SET_BRIGHTNESS, (void*)value);
}

int ScreenGetMaxBrightness(void)
{
    return ioctl(ITP_DEVICE_BACKLIGHT, ITP_IOCTL_GET_MAX_LEVEL, NULL);
}

void ScreenOff(void)
{
    int nRet;
    int nWiFiConnState = 0, nWiFiConnEcode = 0;
    puts("Screen Off!");

#ifdef CFG_POWER_STANDBY
    ioctl(ITP_DEVICE_POWER, ITP_IOCTL_STANDBY, NULL);
#elif defined(CFG_POWER_SLEEP)
#ifdef CFG_NET_WIFI

#endif
    ioctl(ITP_DEVICE_POWER, ITP_IOCTL_SLEEP, NULL);
#else
    ioctl(ITP_DEVICE_BACKLIGHT, ITP_IOCTL_OFF, NULL);
#endif
    screenOff = true;
}

int gScreenOnCounter = 0;
void ScreenOn(void)
{
#ifdef CFG_POWER_STANDBY
    ioctl(ITP_DEVICE_POWER, ITP_IOCTL_RESUME, NULL);
#elif defined(CFG_POWER_SLEEP)
    ioctl(ITP_DEVICE_POWER, ITP_IOCTL_RESUME, NULL);
#ifdef CFG_NET_WIFI
	usleep(100*1000);
	ioctl(ITP_DEVICE_WIFI, ITP_IOCTL_SLEEP, (void *)sleep_to_wakeup);
	//usleep(100*1000);
	/*while (ioctl(ITP_DEVICE_WIFI, ITP_IOCTL_WIFI_SLEEP_STATUS, NULL) != default_no_sleep_or_wakeup){
		//printf("[ScreenOn] wait wifi resume process\n");
		usleep(100*1000);
	}*/
#endif
#else
    ioctl(ITP_DEVICE_BACKLIGHT, ITP_IOCTL_ON, NULL);
#endif
	screenOff = false;
	doubleclick =false;
}

void ScreenSaverRefresh(void)
{
    screenSaverLastTick = SDL_GetTicks();

#if defined(CFG_POWER_WAKEUP_TOUCH_DOUBLE_CLICK)
#else
#endif
    {
        ScreenOn();
    }
}

